package com.rocksea.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.rocksea.app.R;

import butterknife.ButterKnife;

/**
 * Created by 吴志华 on 2017/7/20.
 * activity基类
 */

public abstract class BaseAppCompatActivity extends AppCompatActivity
{

    private View mContent;
    private View mProgressBarView;
    private View mErrorView;
    public Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView();
        ButterKnife.inject(this);
        initBar("ChinaRail");
        intent = getIntent();
        initLayout();
        initView();
        initData();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }
    protected abstract void setContentView();
    protected abstract void initView();
    protected abstract void requrstAgin();
    protected abstract void initData();

    /**
     * 重写设置
     * */
    protected void initBar(String title)
    {
        ActionBar mActionBar = getSupportActionBar();
        if (mActionBar!=null)
        {
            mActionBar.setTitle(title);
            mActionBar.setDisplayHomeAsUpEnabled(true);
        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void initLayout()
    {
        mContent = findViewById(R.id.Main_Content);
        mErrorView = findViewById(R.id.Error_Layout);
        mProgressBarView =findViewById(R.id.ProgressBar);
        TextView mErrorText = (TextView) findViewById(R.id.Error_Text);
        mErrorText.setOnClickListener(onClickListener);
    }

    public void loading()
    {
        mContent.setVisibility(View.GONE);
        mProgressBarView.setVisibility(View.VISIBLE);
        mErrorView.setVisibility(View.GONE);
    }

    public void loadCompleted()
    {
        mContent.setVisibility(View.VISIBLE);
        mProgressBarView.setVisibility(View.GONE);
        mErrorView.setVisibility(View.GONE);
    }

    public void loadFailed()
    {
        mErrorView.setVisibility(View.VISIBLE);
        mProgressBarView.setVisibility(View.GONE);
        mContent.setVisibility(View.GONE);
    }

    private TextView.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            requrstAgin();
        }
    };


    public int getStateBarHeight(Activity a) {
        int result = 0;
        int resourceId = a.getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            result = a.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

}
